package org.han.base.service.impl;

import org.han.base.mapper.BaseMapper;
import org.han.base.quety.TotalQuery;
import org.han.base.service.IBaseService;
import org.han.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Transactional
    @Override
    public void insert(T t) {
        mapper.insert(t);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Transactional
    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public T selectById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public PageList<T> pageList(TotalQuery query) {
        // 总条数
        Long total = mapper.loadTotal(query);
        if(total > 0){
            // 查询到的数据
            List<T> rows = mapper.loadData(query);
            return new PageList(total,rows);
        }
        // 总条数<0直接返回
        return new PageList();
    }

    @Transactional
    @Override
    public void batchDelete(List<Long> ids) {
        mapper.bulkDelete(ids);
    }
}
